package org.hgvc.webapp.ets.jsf.beans.mastersetup;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.DocControl;
import org.hgvc.webapp.ets.constant.AdminConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class DocControlBean extends AbstractBean{
	public static final String BEAN_NAME="docControlBean";
	
	private MasterSetupService masterSetupService;
	private List<DocControl> docControlList;
	private DocControl docControl;
	private boolean catchError;
	private List<SelectItem> docTypeSelectList;
	
	public List<SelectItem> getDocTypeSelectList() {
		return docTypeSelectList;
	}
	public List<DocControl> getDocControlList() {
		return docControlList;
	}
	public void setDocControlList(List<DocControl> docControlList) {
		this.docControlList = docControlList;
	}
	public DocControl getDocControl() {
		return docControl;
	}
	public void setDocControl(DocControl docControl) {
		this.docControl = docControl;
	}
	public void setMasterSetupService(MasterSetupService masterSetupService) {
		this.masterSetupService = masterSetupService;
	}
	
	public void init(){
		try{
			docTypeSelectList = new ArrayList<SelectItem>();
			SelectItem invSelectItem = new SelectItem(CommonConstant.DOC_TYPE_INVOICE,CommonConstant.DOC_TYPE_INVOICE);
			docTypeSelectList.add(invSelectItem);
			SelectItem recSelectItem = new SelectItem(CommonConstant.DOC_TYPE_RECEIPT,CommonConstant.DOC_TYPE_RECEIPT);
			docTypeSelectList.add(recSelectItem);
			SelectItem cnSelectItem = new SelectItem(CommonConstant.DOC_TYPE_CREDIT_NOTE,CommonConstant.DOC_TYPE_CREDIT_NOTE);
			docTypeSelectList.add(cnSelectItem);
			SelectItem ptSelectItem = new SelectItem(CommonConstant.DOC_TYPE_PARTNER_REG,CommonConstant.DOC_TYPE_PARTNER_REG);
			docTypeSelectList.add(ptSelectItem);
			SelectItem agSelectItem = new SelectItem(CommonConstant.DOC_TYPE_AGENT_REG,CommonConstant.DOC_TYPE_AGENT_REG);
			docTypeSelectList.add(agSelectItem);
			SelectItem prSelectItem = new SelectItem(CommonConstant.DOC_TYPE_PROSPECT_REG,CommonConstant.DOC_TYPE_PROSPECT_REG);
			docTypeSelectList.add(prSelectItem);
			SelectItem vcSelectItem = new SelectItem(CommonConstant.DOC_TYPE_VOUCHER,CommonConstant.DOC_TYPE_VOUCHER);
			docTypeSelectList.add(vcSelectItem);
			SelectItem rciAssignSelectItem = new SelectItem(CommonConstant.DOC_TYPE_RCI_ASSIGNMENT,CommonConstant.DOC_TYPE_RCI_ASSIGNMENT);
			docTypeSelectList.add(rciAssignSelectItem);
			SelectItem complimentaryBookingSelectItem = new SelectItem(CommonConstant.DOC_TYPE_COMPLIMENTARY_BOOKING,CommonConstant.DOC_TYPE_COMPLIMENTARY_BOOKING);
			docTypeSelectList.add(complimentaryBookingSelectItem);
			SelectItem rciBookingSelectItem = new SelectItem(CommonConstant.DOC_TYPE_RCI_BOOKING,CommonConstant.DOC_TYPE_RCI_BOOKING);
			docTypeSelectList.add(rciBookingSelectItem);
			SelectItem memberBookingSelectItem = new SelectItem(CommonConstant.DOC_TYPE_MEMBER_BOOKING,CommonConstant.DOC_TYPE_MEMBER_BOOKING);
			docTypeSelectList.add(memberBookingSelectItem);
			
			SelectItem membershipNoSelectItem = new SelectItem(CommonConstant.DOC_TYPE_MEMBERSHIP_NO,CommonConstant.DOC_TYPE_MEMBERSHIP_NO);
			docTypeSelectList.add(membershipNoSelectItem);
			
			docControlList = this.masterSetupService.getDocControlList();
		}catch(BaseException be){
			initErrorBean(be);
		}
	}
	
	public String navAddAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		this.docControl=new DocControl();
		return outcome;
	}
	public String addAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void addActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.docControl.setCreatedBy(getThisUser());
			this.docControl.setCreatedDate(new Date());
			this.docControl.setIsDeleted(CommonConstant.NOT_DELETED);
			this.masterSetupService.saveDocControl(docControl);
			this.docControlList = this.masterSetupService.getDocControlList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String navEditAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void navEditActionEvent(ActionEvent event){
		this.catchError=false;
		UIParameter param = (UIParameter) event.getComponent().findComponent("docControlId");
		String docControlId = (String)param.getValue();
		for(DocControl docControl:this.docControlList){
			if(docControlId.equalsIgnoreCase(docControl.getDocControlId())){
				this.docControl = docControl;
				break;
			}
		}
	}
	
	public String updateAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void updateActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.docControl.setModifiedBy(getThisUser());
			this.docControl.setModifiedDate(new Date());
			this.masterSetupService.updateDocControl(docControl);
			this.docControlList=this.masterSetupService.getDocControlList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String deleteAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void deleteActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			List<DocControl> deleteList = new ArrayList<DocControl>();
			for (DocControl docControl : this.docControlList){
				if (docControl.isMarkAsAction())
					deleteList.add(docControl);
			}
			this.masterSetupService.deleteDocControls(deleteList);
			this.docControlList = this.masterSetupService.getDocControlList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String backToListAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		return outcome;
	}
}
